using System.Collections.Generic;
using System.Linq;
using Nxg.Blog.Core.Documents;

namespace Nxg.Blog.Core.ViewProjections.Admin {
    public class CategoryManageViewModel {
        public IEnumerable<Category> Categories { get; set; }

        public int Page { get; set; }

        public bool HasNextPage { get; set; }

        public bool HasPrevPage => Page > 1;
    }

    public class CategoryManageBindingModel {
        public CategoryManageBindingModel () {
            Page = 1;
            Take = 10;
        }

        public int Page { get; set; }

        public int Take { get; set; }
    }

    public class CategoryManageProjection : IViewProjection<CategoryManageBindingModel, CategoryManageViewModel> {
        private readonly DataContext _db;

        public CategoryManageProjection (DataContext db) {
            _db = db;
        }

        public CategoryManageViewModel Project (CategoryManageBindingModel input) {
            var skip = (input.Page - 1) * input.Take;
            var categories = _db.Category
                .OrderByDescending (x => x.Id)
                .Skip (skip)
                .Take (input.Take + 1)
                .ToList ();

            var pagedPosts = categories.Take (input.Take);
            var hasNextPage = categories.Count > input.Take;

            return new CategoryManageViewModel {
                Categories = pagedPosts,
                    Page = input.Page,
                    HasNextPage = hasNextPage
            };
        }
    }
}